import React, { Component } from 'react';
import myAxios from "../../utils/myaxios"

class test extends Component {
  state = {
    categories: [],
    categories_content: []
  };
  componentDidMount() {
    // 请求数据
    myAxios.get("https://www.linweiqin.cn/api/public/v1/categories")
      .then(res => {
        // categories_cache={}保存响应的数据
        let categories_cache = {
          date: Date.now(),
          categories: res,
        };
        //将categories_cache存入localStorage
        localStorage.setItem("categories", JSON.stringify(categories_cache))
      });

    //再次进入时判断localStorage 内是否有数据
    let category_cache = JSON.parse(localStorage.getItem("category_cache"))
    if (category_cache) {
      //判断是否过期
      let current_time = Date.now();
      let storage_time = category_cache.date;

      if (current_time - storage_time > 1000 * 60 * 60) {
        //数据过期
        myAxios.get("https://www.linweiqin.cn/api/public/v1/categories")
          .then(res => {
            this.state.categories = res.data.message;
            this.state.categories_content = res.data.message[0].children;
          })
      } else {
        // 数据没过期

      }
    } else {
      myAxios.get("https://www.linweiqin.cn/api/public/v1/categories")
        .then(res => {
          this.state.categories = res.data.message;
          this.state.categories_content = res.data.message[0].children;
        })
    }
  }
  render() {

    return (
      <div>

      </div>
    );
  }
}

export default test;
